<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>添加信息</title>
<script type="text/javascript" src="/trm/javascript/jquery-3.2.1.js"></script>
<script type="text/javascript" src="/trm/javascript/window/openwindow.js"></script>
<script type="text/javascript" src="/trm/javascript/select/select.js"></script>
<link rel="stylesheet" href="/trm/javascript/select/select.css" type="text/css" />
<script type="text/javascript" src="/trm/javascript/laydate/laydate.js"></script>
<script type="text/javascript" src="/trm/javascript/Mdate/iScroll.js"></script>
<script type="text/javascript" src="/trm/javascript/Mdate/Mdate.js"></script>
  <script type="text/javascript">
    function addContinue(){
    	
    }
    window.onload = function () {
    	if ("${type}" == "self") {
    		document.getElementById("back").style.display="none";
    	}
    	// 性别
    	var sex = "";
    	sex += "<input type='radio' name='sex' value='男'>男&nbsp;&nbsp;&nbsp;";
    	sex += "<input type='radio' name='sex' value='女'>女";
    	$("#sex").parent().html(sex);
    	//处理课程
    	var courseHtml = "<select id='course' name='course' class=''>";
    	courseHtml += "<option value =''> -- 请选择课程 -- </option>";
    	courseHtml += "<option value ='网络搭建'>网络搭建</option>";
    	courseHtml += "<option value ='网络应用'>网络应用</option>";
    	courseHtml += "<option value ='网络搭建与应用'>网络搭建与应用</option>";
    	courseHtml += "<option value ='信息安全与评估'>信息安全与评估</option>";
    	courseHtml += "</select>";
    	$("#course").parent().html(courseHtml);
    	//处理省、市、县
    	var provinceJson;
    	var cityJson;
    	var areasJson;
    	$.ajax({
    		url:"HtmlServlet?action=provinceList",
    		async:false,
    		dataType: 'text',
    		success: function(responseText){
    			var json = eval( "(" + responseText + ")");
    			provinceJson = json;
    			inputSelect('province', json, 'provinceId');
    		}
    	});
    	$.ajax({
            url:"HtmlServlet?action=cityList",
            async:false,
            dataType: 'text',
            success: function(responseText){
                var json = eval( "(" + responseText + ")");
                cityJson = json;
                //inputSelect('city', json, 'cityId');
            }
        });
    	$.ajax({
            url:"HtmlServlet?action=areasList",
            async:false,
            dataType: 'text',
            success: function(responseText){
                var json = eval( "(" + responseText + ")");
                areasJson = json;
                //inputSelect('county', json, 'areaId');
            }
        });
    	var provinceId;
    	var cityId;
    	$("body").click(function(){
    		if ($("#provinceId") && $("#provinceId").val() != "" && provinceId != $("#provinceId").val()) {
  				provinceId = $("#provinceId").val();
    			var cityDataJson = JSON.parse(JSON.stringify(cityJson));
    			var cityList = cityDataJson.data;
                var cityData = [];
                for (var i = 0; i < cityList.length; i++) {
                	if (cityList[i].provinceid == $("#provinceId").val()) {
                        cityData.push(cityList[i]);
                    }
                }
                cityDataJson.data = cityData;
                inputSelect('city', cityDataJson, 'cityId');
                var areaData = [];
                var countyDataJson = JSON.parse(JSON.stringify(areasJson));
                for (var i = 0; i < cityData.length; i++) {
                    var areaList = countyDataJson.data;
                    for (var j = 0; j < areaList.length; j++) {
                        if (areaList[j].cityid == cityData[i].cityid) {
                            areaData.push(areaList[j]);
                        }
                    }
                }
                //console.log(cityData);
                countyDataJson.data = areaData;
                inputSelect('county', countyDataJson, 'areaId');
                $("#city").val("");
                $("#cityId").val("");
                $("#county").val("");
            }
    		if ($("#cityId") && $("#cityId").val() != "" && cityId != $("#cityId").val()) {
    			cityId = $("#cityId").val();
    			var countyDataJson = JSON.parse(JSON.stringify(areasJson));
                var areaList = countyDataJson.data;
                var areaData = [];
                for (var i = 0; i < areaList.length; i++) {
                    if (areaList[i].cityid == $("#cityId").val()) {
                    	areaData.push(areaList[i]);
                    }
                }
                countyDataJson.data = areaData;
                inputSelect('county', countyDataJson, 'areaId');
                $("#county").val("");
            }
    	});
    	var sUserAgent = navigator.userAgent.toLowerCase();
        var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
        var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
        var bIsMidp = sUserAgent.match(/midp/i) == "midp";
        var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
        var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
        var bIsAndroid = sUserAgent.match(/android/i) == "android";
        var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
        var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
        if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
        	
        	laydate.render({
        	    elem: '#age' //指定元素
	        });
        	
        	//$.date('#age');
        	//new Mdate("age");
        } else {
        	// 移动端不打开键盘
        	$("#age").focus(function(){
      	        document.activeElement.blur();
      	    });
        	// 加载mdate插件
        	new Mdate("age");
        	// 给初始值
        	$("#age").attr("data-year", new Date().getFullYear());
        	$("#age").attr("data-month", new Date().getMonth() + 1);
        	$("#age").attr("data-day", new Date().getDate());
        }
    }
    function submit() {
    	//alert($("#age").val());
    	//$(".text-input").val();
    	//alert($("#province").parent().html());
    	//$("[name$='san'")
    	//$("#course").findoption-selected
    	if ($("input[ name='name']").length > 0) {
    		if ($("input[ name='name']").val() == "") {
                mmy_alert("姓名不能为空！");
                return false;
            }
    	}
    	if ($("input[ name='sex']").length > 0) {
    		if ($('input[name="sex"]:checked').length == 0) {
                mmy_alert("性别不能为空！");
                return false;
            }
        }
    	if ($("input[ name='province']").length > 0) {
            if ($("input[ name='province']").val() == "") {
                mmy_alert("省不能为空！");
                return false;
            }
        }
    	if ($("input[ name='city']").length > 0) {
            if ($("input[ name='city']").val() == "") {
                mmy_alert("市不能为空！");
                return false;
            }
        }
    	if ($("input[ name='county']").length > 0) {
            if ($("input[ name='county']").val() == "") {
                mmy_alert("县（区）不能为空！");
                return false;
            }
        }
    	if ($("input[ name='school']").length > 0) {
            if ($("input[ name='school']").val() == "") {
                mmy_alert("学校不能为空！");
                return false;
            }
        }
    	if ($("input[ name='college']").length > 0) {
            if ($("input[ name='college']").val() == "") {
                mmy_alert("院系不能为空！");
                return false;
            }
        }
    	if ($("input[ name='professional']").length > 0) {
            if ($("input[ name='professional']").val() == "") {
                mmy_alert("专业不能为空！");
                return false;
            }
        }
    	
    	//if ($("input[ name='course']").length > 0) {
            if ($('#course').val() == "") {
                mmy_alert("课程不能为空！");
                return false;
            }
        //}
    	
    	/*
    	if ($("input[ name='grade']").length > 0) {
            if ($("input[ name='grade']").val() == "") {
                mmy_alert("成绩不能为空！");
                return false;
            }
        }
    	*/
    	if ($("input[ name='age']").length > 0) {
            if ($("input[ name='age']").val() == "") {
                mmy_alert("年龄不能为空！");
                return false;
            }
        }
    	if ($("input[ name='phone']").length > 0) {
            if ($("input[ name='phone']").val() == "") {
                mmy_alert("手机不能为空！");
                return false;
            } else {
            	var length = $("input[ name='phone']").val().length;  
                if(length == 11 && /^1[0-9]{10}$/.test($("input[ name='phone']").val()) )   
                {   
                }else{   
                	mmy_alert("手机格式不正确，请重新输入！");
                    return false;   
                }   
            }
        }
    	if ($("input[ name='mail']").length > 0) {
            if ($("input[ name='mail']").val() == "") {
                mmy_alert("邮箱不能为空！");
                return false;
            } else {
            	var email = $("input[ name='mail']").val();
            	if (email != "") {
	            	var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	            	//isok= reg.test(email );
	            	if (!reg.test(email)) {
	            		mmy_alert("邮箱格式不正确，请重新输入！");
	            	    return false;
	            	}
            	}
            }
        }
    	/*
    	if ($("input[ name='note']").length > 0) {
            if ($("input[ name='note']").val() == "") {
                mmy_alert("备注不能为空！");
                return false;
            }
        }
    	*/
    	if ($("input[ name='idcard']").length > 0) {
            if ($("input[ name='idcard']").val() == "") {
                mmy_alert("身份证不能为空！");
                return false;
            } else {
                  var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
                  //isok= reg.test(email );
                  if (!reg.test($("input[ name='idcard']").val())) {
                      mmy_alert("身份证不正确，请重新输入！");
                      return false;
                  }
            }
        }
    	//document.getElementById("form").submit();
    	$.ajax({  
            cache: true,  
            type: "POST",  
            url:"info?action=addUser",  
            data:$('#form').serialize(),// 你的formid  
            async: false,  
            error: function(request) {  
                alert("Connection error:"+request.error);  
            },  
            success: function(data) {  
                mmy_alert("提交成功!");  
            }  
        }); 
    }
    
  </script>
</head>
<body>
  <form id="form" action="info?action=addUser" method="post">
    <!-- 省市县信息 -->
    <input type="hidden" id="provinceId" readonly="true"/>
    <input type="hidden" id="cityId"/>
    <input type="hidden" id="areaId"/>
    <input type="hidden" name="type" value="${type}">
    <input type="hidden" name="id" value="${student.id }">
	<table id="table">
	<%List<Map<String, Object>> properties = (List<Map<String, Object>>) request.getAttribute("properties"); 
    Map<String, Object> student  = (Map<String, Object>) request.getAttribute("student");%>
        <%for (Map<String, Object> property : properties) {
            String propertyName = property.get("property") != null ? property.get("property").toString() : "";
            String studentProperty = "";
            if (student != null) {
                studentProperty = student.get(propertyName) != null ? student.get(propertyName).toString() : "";
            }%>
      <tr>
        <td><%=property.get("name") %>：</td>
        <td><input id="<%=propertyName%>" name="<%=propertyName%>" value="<%=studentProperty%>"></td>
      </tr>
        <%}%>
	</table>
	<!-- <a onclick="addContinue">继续添加</a><br> -->
	<input type="hidden" name="role" value="${role}"/>
	<a style="cursor:pointer;color:blue;" onclick="submit();">确认</a>
	<a id="back" style="text-decoration:none" href="javascript:history.go(-1)">返回</a>
  </form>
</body>
</html>